package com.gexiaobao.pigeon.ui.breeding_pigeon

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import android.widget.LinearLayout
import android.widget.ListPopupWindow
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.model.bean.DaysBean
import com.gexiaobao.pigeon.app.model.bean.ReminderListBean
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.databinding.ActivitySetReminderBinding
import com.gexiaobao.pigeon.ui.adapter.AdapterReminder
import com.gexiaobao.pigeon.ui.adapter.AddBloodPigeonPopAdapter
import com.gexiaobao.pigeon.ui.view.CustomItemDecoration
import com.gexiaobao.pigeon.viewmodel.BreedingPigeonViewModel
import com.google.gson.Gson
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody

/**
 * @Author      : hxw
 * @Date        : 2024/9/6 15:01
 * @Describe    : 设置提醒
 */
class ActivitySetReindeer : BaseActivity<BreedingPigeonViewModel, ActivitySetReminderBinding>() {

    private val mAdapter: AdapterReminder by lazy { AdapterReminder() }

    private val popWindow: ListPopupWindow by lazy { ListPopupWindow(this) }
    private var adapterDays: AddBloodPigeonPopAdapter? = null
    private var mListDay: ArrayList<String> = arrayListOf()
    private var mListBean: ArrayList<DaysBean> = arrayListOf()
    private var popPosition = 0
    private var mCategoryId = 0
    private var mId = 0

    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setTranslucentForImageView(this, 0, null)
        mDatabind.includeBar.tvTitle.text = getString(R.string.Tea_Timer)

        mViewModel.getBreedingPairSetting()

        mDatabind.swipeRecyclerView.init(LinearLayoutManager(this), mAdapter)
        mDatabind.swipeRecyclerView.addItemDecoration(CustomItemDecoration(1, ContextCompat.getColor(this, R.color.comm_line)))

        mAdapter.addChildClickViewIds(R.id.tvItemDaySet)
        mAdapter.setOnItemChildClickListener { _, view, position ->
            when (view.id) {
                R.id.tvItemDaySet -> setDaysPopList(view, mAdapter.data[position], position)
            }
        }

        popWindow.setOnItemClickListener { _, _, position, _ ->
            popPosition = position
            adapterDays!!.setCheckItem(position)
            popWindow.dismiss()
            editPairReminder()
        }
    }

    private fun editPairReminder() {
        val map = mapOf(
            "id" to mId,
            "categoryId" to mCategoryId,
            "value" to mListBean[popPosition].info
        )
        val body: RequestBody = Gson().toJson(map).toRequestBody("application/json".toMediaTypeOrNull())
        showLoading(getString(R.string.loading))
        mViewModel.editBreedingPairSettingToApp(body)
    }

    private fun setDaysPopList(view: View, reminderListBean: ReminderListBean, position: Int) {
        mCategoryId = mAdapter.data[position].categoryId
        if (reminderListBean.list.isNotEmpty()) {
            mListBean.clear()
            mListBean.addAll(reminderListBean.list)
            mListDay.clear()
            reminderListBean.list.forEach {
                mListDay.add(it.info)
            }
        }
        adapterDays = AddBloodPigeonPopAdapter(this, mListDay)
        popWindow.setAdapter(adapterDays)
        popWindow.anchorView = view//设置ListPopupWindow的锚点，关联mButton位置
        popWindow.width = LinearLayout.LayoutParams.WRAP_CONTENT
        popWindow.height = LinearLayout.LayoutParams.WRAP_CONTENT
        popWindow.isModal = true
        popWindow.show()
        adapterDays!!.setCheckItem(popPosition)
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun createObserver() {
        super.createObserver()
        /** 编辑作育提醒 */
        mViewModel.editBreedingPairResult.observe(this) {
            mViewModel.getBreedingPairSetting()
        }
        /** 获取作育提醒设置 */
        mViewModel.reminderResult.observe(this) {
            mId = it.id
            mAdapter.setList(it.list)
            mAdapter.notifyDataSetChanged()
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.includeBar.ivBack) {
            when (it) {
                mDatabind.includeBar.ivBack -> finish()
            }
        }
    }
}